<!DOCTYPE html>
<html lang="zh" xmlns:th="https://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增分类')"/>
</head>

<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="categoryAddForm" name="form-config-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">名称：</label>
            <div class="col-sm-8">
                <input id="categoryTitle" name="categoryTitle" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <textarea name="description" class="form-control"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">权重：</label>
            <div class="col-sm-8">
                <input id="configValue" name="weight" class="form-control" type="number">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">推荐：</label>
            <div class="col-sm-8">
                <div class="onoffswitch">
                    <input type="checkbox" th:checked="false" class="onoffswitch-checkbox" id="support">
                    <label class="onoffswitch-label" for="support">
                        <span class="onoffswitch-inner"></span>
                        <span class="onoffswitch-switch"></span>
                    </label>
                </div>
            </div>
        </div>
        <div class="mail-body text-center tooltip-demo">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i
                    class="fa fa-check"></i>保 存
            </button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i
                    class="fa fa-reply-all"></i>关 闭
            </button>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    var prefix = ctx + "blog/category";

    $("#categoryAddForm").validate({
        rules: {
            categoryTitle: {
                required: true,
                rangelength: [2, 15],
                remote: {
                    url: prefix + "/checkCategoryTitleUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        title: function () {
                            return $.common.trim($("#categoryTitle").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            description: {
                required: true,
                rangelength: [2, 80]
            },
            weight: {
                required: true,
                number: true
            },
        },
        messages: {
            "categoryTitle": {
                required: "请输入标题",
                rangelength: "请输入长度为 {0} 至 {1} 之间的标题",
                remote: "标题已存在"
            },
            "description": {
                required: "请输入标题",
                rangelength: "请输入长度为 {0} 至 {1} 之间的标题"
            },
            "weight": {
                required: "请输入权重",
                rangelength: "请输入正确格式的权重"
            },
        },
    });

    function submitHandler() {
        if ($.validate.form()) {
            var support = $("input[id='support']").is(':checked') == true ? 1 : 0;
            var data = $('#categoryAddForm').serializeArray();
            data.push({"name": "support", "value": support})
            $.operate.saveTab(prefix + "/add", data);
        }
    }
</script>
</body>
</html>
